#include "externals.h"
#include "shapes.h"
#include <math.h>

void draw_cube(float size)
{
	float cs = size/2.0;
	glBegin(GL_QUAD_STRIP);
		// front
		glColor3f(1, 0, 0);	
		glVertex3f(-cs, cs, cs);
		glVertex3f(-cs, -cs, cs);
		glVertex3f(cs, cs, cs);
		glVertex3f(cs, -cs, cs);
		// right
		glColor3f(0, 1, 0);	
		glVertex3f(cs, cs, -cs);
		glVertex3f(cs, -cs, -cs);
		// back
		glColor3f(0, 0, 1);	
		glVertex3f(-cs, cs, -cs);
		glVertex3f(-cs, -cs, -cs);
		// left
		glColor3f(0, 1, 1);	
		glVertex3f(-cs, cs, cs);
		glVertex3f(-cs, -cs, cs);
	glEnd();
	glBegin(GL_QUADS);
		// top
		glColor3f(1, 1, 0);	
		glVertex3f(-cs, cs, cs);
		glVertex3f(cs, cs, cs);
		glVertex3f(cs, cs, -cs);
		glVertex3f(-cs, cs, -cs);
		// bottom
		glColor3f(1, 0, 1);	
		glVertex3f(-cs, -cs, cs);
		glVertex3f(-cs, -cs, -cs);
		glVertex3f(cs, -cs, -cs);
		glVertex3f(cs, -cs, cs);
	glEnd();
}

void draw_tetr(float size)
{
	glBegin(GL_TRIANGLE_FAN);
	glColor3f(1, 0, 0);	
			glVertex3f(2, 2, 2);
			glVertex3f(-2, 2, -2);
			glVertex3f(-2, -2, 2);
			// right
			
			glColor3f(0, 1, 0);	
			glVertex3f(2, -2, -2);
			// back
			glColor3f(0, 0, 1);	
			glVertex3f(-2, 2, -2);						
		glEnd();
	glBegin(GL_TRIANGLES);	
			glColor3f(1, 0, 1);
			glVertex3f(-2, -2, 2);
			glVertex3f(-2, 2, -2);
			glVertex3f(2, -2, -2);
	glEnd();
}
void draw_oct(float size)
{
	glBegin(GL_TRIANGLE_FAN);
		glColor3f(1, 0, 0);	
		glVertex3f(1, 1.4, 1);
		glVertex3f(0, 0, 0);
		glVertex3f(0, 0, 2);
		// right
		glColor3f(0, 1, 0);	
		glVertex3f(2, 0, 2);
		// back
		glColor3f(0, 1, 1);	
		glVertex3f(2, 0, 0);
		
		glColor3f(1, 1, 1);	
		glVertex3f(0, 0, 0);
	glEnd();
	
	//bottom triangle
glBegin(GL_TRIANGLE_FAN);	
glColor3f(1, 0.5, 0);	
		glVertex3f(1, -1.4, 1);
		glVertex3f(0, 0, 2);
		glVertex3f(0, 0, 0);
		
		glColor3f(0, 1, 0.5);	
		glVertex3f(2, 0, 0);
		
		glColor3f(0.5, 1, 0);	
		glVertex3f(2, 0, 2);
			
		glColor3f(0.5, 1, 1);	
		glVertex3f(0, 0, 2);			
	glEnd();
	
}
void draw_conus(float size)
{
	float x,y,pi = 3.14;
	int r = 8; 
	glBegin(GL_TRIANGLE_FAN);
		glColor3f(1, 0, 0);	
		glVertex3f(0, 0, 10);
		for (int i = -180; i <= 180; i++) {
			
			x = r*cos(pi*i/180);
			y = r*sin(pi*i/180);
			glColor3f((int)x, (int)y, 0.5);
			glVertex3f(x, y, 0);
			x = r*cos(pi*(i+1)/180);
			y = r*sin(pi*(i+1)/180);
			glVertex3f(x, y, 0);
		}
	glEnd();
	glBegin(GL_POLYGON);
			glColor3f(1, 1, 0);	
			for (int i = 180; i >= -180; i--) {
				x = r*cos(pi*i/180);
				y = r*sin(pi*i/180);
				glVertex3f(x, y, 0);
			}
		glEnd();
}

void draw_cyl(float h, float r, int n)
{
	
	   //верх
	       glBegin(GL_TRIANGLE_FAN);
	       glColor3f (1, 0, 1);
	       glVertex3f (0, 0, h);
	       for (int i=0; i<=n; i++)
	       {

	               float a=(i==n) ? 0 : ((2*3.14159)/n)*i;
	               float x=cos(a)*r;
	               float y=sin(a)*r;
	               if (i&&i%2)
	                       glColor3f(1, 0, 0);
	               else
	                       glColor3f(1, 0, 1);
	               glVertex3f(x, y, h);
	       }
	       glEnd();


	       //дно
	       glBegin(GL_TRIANGLE_FAN);
	               glColor3f (1, 0, 1);
	               glVertex3f (0, 0, 0);
	               for (int i=0; i<=n; i++)
	               {

	                       float a=(i==n) ? 0 : ((2*3.14159)/n)*i;
	                       float x=cos(-a)*r;
	                       float y=sin(-a)*r;
	                       if (i&&i%2)
	                               glColor3f(1, 0, 1);
	                       else
	                               glColor3f(1, 0, 0);
	                       glVertex3f(x, y, 0);
	               }
	               glEnd();

	               //сторона
	               glBegin(GL_TRIANGLE_STRIP);
	               glColor3f (1, 0, 1);
	               for (int i=0; i<=n; i++)
	               {

	                       float a=(i==n) ? 0 : ((2*3.14159)/n)*i;
	                       float x=cos(a)*r;
	                       float y=sin(a)*r;
	                       if (i&&i%2)
	                               glColor3f(1, 0, 0);
	                       else
	                       glColor3f(1, 0, 1);
	                       glVertex3f(x, y, h);
	                       glVertex3f(x, y, 0);
	               }
	               glEnd();

}
void draw_elka(int * array, int ns)

{
		//массив координат
      // float points[]={4,0,2,2,3,2,1,4,2,4,0,6};
       
       int n = (sizeof(points)/sizeof(points[0]))/2;
       int np = n * (ns + 1);
       float * vp = new float [np * 3];
       float * ptr = vp;
       for (int i=0; i<n; i++)
       {
                       float r = points[i*2];
                       float h = points[i*2 + 1];
                       
               for (int j=0; j<=ns; j++ )
               {
                       float a=(j==ns) ? 0 : ((2*3.14159)/ns)*j;
                       *ptr++ = r*cos(a);
                       *ptr++ = h;
                       *ptr++ = r*sin(a);
               }
       }
       ptr = vp;
       for (int t = 0; t < (n-1); t++)
       {		
 	   		glColor3f(0, 1, 0);
               glBegin(GL_TRIANGLE_STRIP);
               float * v0 = ptr;
               float * v1 = v0 + (ns + 1)*3;
               for (int i=0; i<=ns; i++)
               {
                       glVertex3fv(v0);
                       glVertex3fv(v1);
                       v1 += 3;
                       v0 += 3;
               }
               ptr += ((ns + 1)*3);
       }
       glEnd();
       delete []vp;
}
